$(function () {
    $("#tab-first").click(function () {
        $("#searchInputSend").val(null)
        $("#pane-second").hide();
        $("#pane-first").show();
        $(".el-tabs_active").css("margin-left","20px");
        $("#tab-first").css("color","#409eff");
        $("#tab-second").css("color","#000000")

        //点击send
            $(".list").remove()
            $.ajax({
                url: url +"/userPageInfo/selectSendUserPageInfo",
                async: false,
                dataType: "json",
                type: "post",
                success: function (data) {
                    let list = data.data
                    if (null == list) {
                        console.log("没有数据")
                        return
                    }
                    let html = ''
                    console.log(data)
                    for (let i = 0; i < list.length; i++) {
                        let status = list[i].status
                        let pageNum = list[i].pageNum
                        html += `<tr class="list">
                  <td>${list[i].pageNum}</td>
                <td>${list[i].sendName}</td>
                <td>${list[i].receiveName}</td>
                <td class="status" id=${status}>${list[i].status}</td>
                <td><input type="button" class="sendDetail" id=${pageNum} data-target="#myModal1" data-toggle="modal" value="订单详情"></td></tr>`
                    }
                    $(".pageTable").append(html)
                }
            })

    })
    $("#tab-second").click(function () {
        $("#searchInputReceive").val(null)
        $("#pane-first").hide();
        $("#pane-second").show();
        $(".el-tabs_active").css("margin-left","110px");
        $("#tab-second").css("color","#409eff");
        $("#tab-first").css("color","#000000");
        
        $(".list").remove()
        $.ajax({
            url: url + "/userPageInfo/selectReceiveUserPageInfo",
            async: false,
            dataType: "json",
            type: "post",
            success: function (data) {
                console.log(data)
                let list = data.data
                if (null == list) {
                    console.log("没有数据")
                    return
                } else {
                    let html = ''
                    for (let i = 0; i < list.length; i++) {
                        let status = list[i].status
                        let pageNum = list[i].pageNum
                        html += `<tr class="list">
                <td>${list[i].pageNum}</td>
                <td>${list[i].sendName}</td>
                <td>${list[i].receiveName}</td>
                <td class="status" id=${status}>${list[i].status}</td>
                <td><input type="button" class="receiveDetail" id=${pageNum} data-target="#myModal1" data-toggle="modal" value="订单详情"></td></tr>`
                    }
                    $(".pageTable").append(html)
                }
            }
        })
    })

})

$.ajax({
    url: url +"/userPageInfo/selectSendUserPageInfo",
    async: false,
    dataType: "json",
    type: "post",
    success: function (data) {
        let html = ''
        console.log(data)
        var list = data.data
        if(null == list){
            console.log("没有数据")
            return
        }
        for (let i = 0; i < list.length; i++) {
            let status = list[i].status
            let pageNum = list[i].pageNum
            html += `<tr class="list">
                <td>${list[i].pageNum}</td>
                <td>${list[i].sendName}</td>
                <td>${list[i].receiveName}</td>
                <td class="status" id=${status}>${list[i].status}</td>
                <td><input type="button" class="sendDetail" id=${pageNum} data-target="#myModal1" data-toggle="modal" value="订单详情"></td></tr>`
        }
        $(".pageTable").append(html)
    }
})

// 快递详情
$("body").on("click", ".sendDetail", function () {
    let pageNum = $(this).attr("id")
    console.log(pageNum)
    $.post(url +"/userPageInfo/userDetailPageInfo", "pageNum=" + pageNum, function (data) {
        console.log(data)
        if ("已下单" == data.data.status||"待抢单" == data.data.status){
            //根据status 变换模态框的第一个按钮的显示，以及点击后的显示
            $("#deleteOrder").text("取消订单")
            $("#deleteOrder").attr("id", "cancelOrder")
            $("#deleteOrder").attr("style", "")
            $("#cancelOrder").text("取消订单")
            $("#cancelOrder").attr("id", "cancelOrder")
            $("#cancelOrder").attr("style", "")
            $("#text").text("此操作将取消该订单, 是否继续?")
            //根据status 变换“确定”按钮的id，进而确定功能
            $("#ensure").attr("id", "cancelEnsure")
            $("#deleteEnsure").attr("id", "cancelEnsure")
        } else if ("已取消" == data.data.status) {
            $("#deleteOrder").text("删除订单")
            $("#deleteOrder").attr("id", "deleteOrder")
            $("#deleteOrder").attr("style", "")
            $("#cancelOrder").text("删除订单")
            $("#cancelOrder").attr("id", "deleteOrder")
            $("#cancelOrder").attr("style", "")
            $("#text").text("此操作将永久删除该订单, 是否继续?")
            $("#ensure").attr("id", "deleteEnsure")
            $("#cancelEnsure").attr("id", "deleteEnsure")
        } else if ("运输中" == data.data.status ||"已完成" == data.data.status||"待送达" == data.data.status) {
            $("#deleteOrder").attr("style", "display:none")
            $("#cancelOrder").attr("style", "display:none")
        } else if ("待取件" == data.data.status) {
            $("#deleteOrder").attr("style", "display:none")
            $("#cancelOrder").attr("style", "display:none")
        } else if ("已签收" == data.data.status) {
            $("#deleteOrder").attr("style", "display:none")
            $("#cancelOrder").attr("style", "display:none")
        }
        $("#pageNum").text("快递编号: " + data.data.pageNum)
        $("#status").text("最新状态: " + data.data.status)
        $("#sNameAndPhone").text(data.data.sendName + " " + data.data.sendPhone)
        $("#sAddressAndDetailAddress").text(data.data.sendProvince + data.data.sendCity + data.data.sendArea + " " + data.data.sendDetail)
        $("#rNameAndPhone").text(data.data.receiveName + " " + data.data.receivePhone)
        $("#rAddressAndDetailAddress").text(data.data.receiveProvince + data.data.receiveCity + data.data.receiveArea + " " + data.data.receiveDetail)
        $("#createTime").text(data.data.createTime)
    }, "json")

    //点击删除订单记录后并且点击确定
    $("body").on("click","#deleteEnsure",function () {
        $("#myModal2").modal("hide")
        $("#myModal1").modal("hide")
        $.post(url +"/userPageInfo/deleteCancelPage", "pageNum=" + pageNum, function (data) {
            $(".list").remove()
            $.ajax({
                url: url+"/userPageInfo/selectSendUserPageInfo",
                async: false,
                dataType: "json",
                type: "post",
                success: function (data) {
                    let list = data.data
                    if (null == list) {
                        console.log("没有数据")
                        return
                    }
                    let html = ''
                    console.log(data)
                    for (let i = 0; i < list.length; i++) {
                        let status = list[i].status
                        pageNum = list[i].pageNum
                        html += `<tr class="list">
                <td>${list[i].pageNum}</td>
                <td>${list[i].sendName}</td>
                <td>${list[i].receiveName}</td>
                <td class="status" id=${status}>${list[i].status}</td>
                <td><input type="button" class="sendDetail" id=${pageNum} data-target="#myModal1" data-toggle="modal" value="订单详情"></td></tr>`
                    }
                    $(".pageTable").append(html)
                }
            })
        })
    })

    //点击取消订单记录后并且点击确定
    $(document).on("click","#cancelEnsure",function () {
        $("#myModal2").modal("hide")
        $("#myModal1").modal("hide")
        $.post(url +"/userPageInfo/cancelOrderPage", "pageNum=" + pageNum, function (data) {
            console.log(data)
            $(".list").remove()
            $.ajax({
                url: url+"/userPageInfo/selectSendUserPageInfo",
                async: false,
                dataType: "json",
                type: "post",
                success: function (data) {
                    let list = data.data
                    if (null == list) {
                        console.log("没有数据")
                        return
                    }
                    let html = ''
                    console.log(data)
                    for (let i = 0; i < list.length; i++) {
                        let status = list[i].status
                        pageNum = list[i].pageNum
                        html += `<tr class="list">
                <td>${list[i].pageNum}</td>
                <td>${list[i].sendName}</td>
                <td>${list[i].receiveName}</td>
                <td class="status" id=${status}>${list[i].status}</td>
                <td><input type="button" class="sendDetail" id=${pageNum} data-target="#myModal1" data-toggle="modal" value="订单详情"></td></tr>`
                    }
                    $(".pageTable").append(html)
                }
            })
        }, "json")
    })
    //点击再寄一单
    $("#sendAgain").click(function () {
        $.post(url +"/userPageInfo/userDetailPageInfo", "pageNum=" + pageNum, function (data) {
            //将数据传给预约寄件
            window.location.href = "booking_delivery.html?data=" + data.data
        }, "json")
    })
})

//我收的 收到快递的详情
$("body").on("click", ".receiveDetail", function () {
    let pageNum = $(this).attr("id")
    console.log(pageNum)
    $.post(url +"/userPageInfo/userDetailPageInfo", "pageNum=" + pageNum, function (data) {
        console.log(data)
        //不能进行操作
        $("#deleteOrder").attr("style", "display:none")
        $("#cancelOrder").attr("style", "display:none")
        $("#sendAgain").attr("style", "display:none")

        $("#pageNum").text("快递编号: " + data.data.pageNum)
        $("#status").text("最新状态: " + data.data.status)
        $("#sNameAndPhone").text(data.data.sendName + " " + data.data.sendPhone)
        $("#sAddressAndDetailAddress").text(data.data.sendProvince + data.data.sendCity + data.data.sendArea + " " + data.data.sendDetail)
        $("#rNameAndPhone").text(data.data.receiveName + " " + data.data.receivePhone)
        $("#rAddressAndDetailAddress").text(data.data.receiveProvince + data.data.receiveCity + data.data.receiveArea + " " + data.data.receiveDetail)
        $("#createTime").text(data.data.createTime)
    }, "json")
})

//根据输入框内容 查询我寄的
$("#selectSend").click(function () {
    let searchValue = $("#searchInputSend").val()
    console.log(searchValue)
    $.post(url+"/userPageInfo/selectSendUserPageInfo", "searchValue=" + searchValue, function (data) {
        $(".list").remove()
        let list = data.data
        console.log(data)
        if (null == list) {
            console.log("没有数据")
            return
        }
        let html = ''
        for (let i = 0; i < list.length; i++) {
            let status = list[i].status
            let pageNum = list[i].pageNum
            html += `<tr class="list">
                <td>${list[i].pageNum}</td>
                <td>${list[i].sendName}</td>
                <td>${list[i].receiveName}</td>
                <td class="status" id=${status}>${list[i].status}</td>
                <td><input type="button" class="sendDetail" id=${pageNum} data-target="#myModal1" data-toggle="modal" value="订单详情"></td></tr>`
        }
        $(".pageTable").append(html)
    },"json")
})

//根据输入框内容 查询我收的
$("#selectReceive").click(function () {
    let searchValue = $("#searchInputReceive").val()
    $.post(url+"/userPageInfo/selectReceiveUserPageInfo", "searchValue=" + searchValue, function (data) {
        $(".list").remove()
        let list = data.data
        if (null == list) {
            console.log("没有数据")
            return
        }
        let html = ''
        console.log(data)
        for (let i = 0; i < list.length; i++) {
            let status = list[i].status
            let pageNum = list[i].pageNum
            html += `<tr class="list">
                <td>${list[i].pageNum}</td>
                <td>${list[i].sendName}</td>
                <td>${list[i].receiveName}</td>
                <td class="status" id=${status}>${list[i].status}</td>
                <td><input type="button" class="sendDetail" id=${pageNum} data-target="#myModal1" data-toggle="modal" value="订单详情"></td></tr>`
        }
        $(".pageTable").append(html)
    }, "json")
})